<template>
  <div class="whole">
    <el-container>
      <el-aside width="200px">
        <CommonAside />
      </el-aside>
      <el-container>
        <el-header>
          <ComponentsHeader />
        </el-header>
        <el-main>
          <div class="app-container">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
              <el-form-item label="客户端编号" prop="clientNo">
                <el-input
                    v-model="queryParams.clientNo"
                    placeholder="请输入客户端编号"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="客户端名字" prop="clientName">
                <el-input
                    v-model="queryParams.clientName"
                    placeholder="请输入客户端名字"
                    clearable
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8" style="margin-bottom: 8px">
              <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                >删除
                </el-button>
              </el-col>

              <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
            </el-row>

            <el-table v-loading="loading" :data="clientList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center"/>
              <el-table-column label="客户端编号" align="center" prop="clientNo"/>
              <el-table-column label="客户端名字" align="center" prop="clientName"/>
              <el-table-column label="创建时间" align="center" prop="createTime"/>
              <el-table-column label="修改时间" align="center" prop="updateTime"/>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                  >修改
                  </el-button>
                  <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                  >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

            <!--            //分页-->
            <div class="block" style="float: right; margin-top: 24px;">

              <el-pagination
                  v-show="total>0"
                  background
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :page-sizes="[5, 10, 15, 20]"
                  :page-size="queryParams.pageSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total">
              </el-pagination>
            </div>


            <!--    <pagination-->
            <!--        v-show="total>0"-->
            <!--        :total="total"-->
            <!--        :page.sync="queryParams.pageNum"-->
            <!--        :limit.sync="queryParams.pageSize"-->
            <!--        @pagination="getList"-->
            <!--    />-->

            <!-- 添加或修改智造双碳-双碳配置-固定碳排放对话框 -->
            <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
              <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="客户端编号" prop="clientNo">
                  <el-input v-model="form.clientNo" placeholder="请输入汽车品牌"/>
                </el-form-item>
                <el-form-item label="客户端名字" prop="clientName">
                  <el-input v-model="form.clientName" placeholder="请输入客户端名字"/>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
              </div>
            </el-dialog>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import {
  listClient,
  getClient,
  delClient,
  updateClient
} from "@/api/client";
import CommonAside from "@/components/CommonAside.vue";
import ComponentsHeader from "@/components/ComponentsHeader.vue";

export default {
  name: "Client",
  components: {ComponentsHeader, CommonAside},
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      clientList: [
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        clientNo: [
          {required: true, message: "客户端编号不能为空", trigger: "blur"},
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
        clientName: [
          {required: true, message: "客户端名字不能为空", trigger: "blur"},
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listClient(this.queryParams).then(response => {
        console.log(response)
        this.clientList = response.data.data.data;
        this.total = response.data.data.totalRecords;
        this.loading = false;
        console.log(response)
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clientName:null,
        clientNo:null

      };
      this.$refs["queryForm"].resetFields();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs["queryForm"].resetFields();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加汽车模型";
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
       getClient(id).then(response => {

         this.form = response.data.data;
      this.open = true;
      this.title = "修改客户端";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            updateClient(this.form).then(response => {
              console.log(response)
              this.$message({
                    type: 'success',
                    message: '修改成功!'
                  }
              );
              this.open = false;
              this.getList();
            });
          }

      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids.toString();
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delClient(ids).then(res=>{
          if(res.data.code==200){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList()
          }else{
            this.$message({
              type: 'warning',
              message: '删除失败'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });


    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dc/client/export', {
        ...this.queryParams
      }, `client_${new Date().getTime()}.xlsx`)
    },
    //分页相关方法
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.page = val;
      this.getList()
    },
  }
};
</script>
